package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @RequestMapping("query")
    @ResponseBody
    public Object query(TravelQuery query){
        return JsonResult.success(travelService.queryByDestId(query));
    }

    @RequestMapping("input")
    @ResponseBody
    public Object input(String id){
        Map<String,Object> map = new HashMap<>();
        if(StringUtils.hasLength(id)){
            Travel travel = travelService.findById(id);
            map.put("tv",travel);
        }
        List<Destination> destinations = destinationService.list();
        map.put("dests",destinations);
        return JsonResult.success(map);
    }
    @RequireLogin   //使用原本的方法 进行登录检查
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    @ResponseBody
    public Object saveOrUpdate(Travel travel, HttpServletRequest request){
        //2:当前登录用户信息
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        if(user != null){
            travel.setUserId(user.getId());
        }
        //最新游记id
        String id = travelService.saveOrUpdate(travel);
        return JsonResult.success(id);
    }
    //但是这里有问题是每次进行登录操作都要使用request然后进行登录加满
    //因此应当解压参数解析器来解决这个问题

    @RequireLogin
    @RequestMapping(value = "/commentAdd",method = RequestMethod.POST)
    @ResponseBody
    public Object commentAdd(TravelComment comment, @UserParam UserInfo userInfo){

        //用户游记评论
        BeanUtils.copyProperties(userInfo, comment);
        System.out.println(userInfo);
        comment.setUserId(userInfo.getId());
        travelCommentService.addComment(comment);

        return JsonResult.success();
    }

    @RequestMapping(value = "/comments",method = RequestMethod.GET)
    @ResponseBody
    public Object comments(String travelId){
//        System.out.println(travelCommentService.queryByTravelId(travelId));
        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    @ResponseBody
    public Object detail(String id,String destId){
        Travel travel = travelService.findById(id);
//        System.out.println(travel.getAuthor().getId());
        return JsonResult.success(travel);
    }

//    //问题: 每次进行获取当前登录用户信息,都需要使用request 获取token, 然后再通过token获取用户信息,
//    //代码实现麻烦
//
//    //思考: 是否可以通过参数注入的方式实现呢?
//    //答案是yes, 需要使用springmvc 参数解析器知识点, 以前用户参数解析器: 文件上传参数解析器
//    // springmvc 没有提供当前登录用户参数解析器, 需要我们自己定义.
//
//    //自定义参数解析器, 在请求方法参数中自动注入当前登录用.
//    //获取当前登录用户信息
//    @RequestMapping(value = "/info",method = RequestMethod.GET)
//    @ResponseBody
//    public Object info(@UserParam UserInfo userInfo){
//        //userInfo  就是当前登录用户对象
//        return JsonResult.success(userInfo);
//    }
//    //如果说需要收集页面传过来用户信息时, 此时也需要注入用户对象
//    //存在问题了: 此时使用自定义用户解析器, 还是用springmvc默认参数解析器?
//    //问题核心: 解析的参数存在区分对待了, 联想到 标签区分
//    //解决方案: 对解析参数贴@UserParam 进行区分解析.
//    @RequestMapping(value = "/userUpdate",method = RequestMethod.GET)
//    @ResponseBody
//    public Object update(UserInfo userInfo){
//        //userInfo  就是当前登录用户对象
//        return JsonResult.success(userInfo);
//    }
}
